package com.hisense.ovcloud.spark.sql.config;

import com.mybatisflex.core.audit.AuditManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class MybatisFlexConfig {
    @Value("${spring.flexLogEnable:true}")
    private boolean mybatisFlexLogEnable;

    @PostConstruct
    public void enableLog(){
        if(mybatisFlexLogEnable){
            //开启审计功能
            AuditManager.setAuditEnable(true);

            //设置 SQL 审计收集器
            AuditManager.setMessageCollector(auditMessage ->
                    log.info("{},{}ms", auditMessage.getFullSql()
                            , auditMessage.getElapsedTime())
            );
        }
    }
}
